package com.ptt.sqm.activity;

import java.util.Date;
import java.util.List;

import android.R.menu;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.pm.ActivityInfo;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.WindowManager.LayoutParams;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ptt.sqm.component.MenuList;
import com.ptt.sqm.component.Search;
import com.ptt.sqm.model.Login;
import com.ptt.sqm.model.RoleMenu;
import com.ptt.sqm.session.SessionManager;
import com.ptt.sqm.util.AppUtil;
import com.ptt.sqm.util.DateDeserializer;
import com.ptt.sqm.util.ListDeserializer;
import com.ptt.sqm.ws.WebserviceCall;
import com.ptt.sqmonmobile.R;

public class DefaultActivity extends Activity {

	// Session Manager
	SessionManager session;

	// Create Webservice class object
	WebserviceCall com;
	Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new DateDeserializer()).registerTypeAdapter(List.class, new ListDeserializer()).create();

	// Create Menu List
	MenuList menuList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		// Disable rotate screen
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

		// Session class instance
		session = new SessionManager(getApplicationContext());
		if (!(this instanceof LoginActivity))
			session.checkLogin();

		com = WebserviceCall.getInstance(getString(R.string.ws_url), getString(R.string.ws_namespace), session, this);

		// Create Menu List
		if (!(this instanceof LoginActivity)) {
			Login login = session.getUserDetails();

			MenuList.reset();
			menuList = MenuList.getInstance();

			int pos = 0;
			if (login.Menus != null) {
				for (RoleMenu role : login.Menus.get(0).RoleMenu) {
					if (menuList.Frm_ShowAssessData.equals(role.Menu_Url))
						menuList.setPosFrm_ShowAssessData(++pos);
					else if (menuList.Frm_AssessData.equals(role.Menu_Url))
						menuList.setPosFrm_AssessData(++pos);
					else if (menuList.Frm_StationInfo.equals(role.Menu_Url))
						menuList.setPosFrm_StationInfo(++pos);
					else if (menuList.Rpt_AssessPoint.equals(role.Menu_Url))
						menuList.setPosRpt_AssessPoint(++pos);
					else if (menuList.Rpt_AssessComment.equals(role.Menu_Url))
						menuList.setPosRpt_AssessComment(++pos);
				}
			}

			menuList.setPosLogout(++pos);
		}

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		Log.i("Session" + this, "on Stop");

	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		Log.i("Session" + this, "on Restart");
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.i("Session" + this, "on Resume");
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Log.i("Session" + this, "on Pause");
	}

}
